const checkBloodSugarStatus = (data, standards) => {
	// 从数据中提取value和time
	const bloodSugarValue = parseFloat(data.value);
	const time = new Date(data.time);
	const currentHour = time.getHours();
	const currentMinute = time.getMinutes();
	// 将三餐时间转换为分钟数，方便计算
	const breakfastTime =  parseBreakfastTime(standards.breakfastTime);
	const lunchTime = parseBreakfastTime(standards.lunchTime);
	const dinnerTime = parseBreakfastTime(standards.dinnerTime);
	// 将时间窗口（一小时和两小时）转换为分钟数
	const oneHourAfter = 60;
	const twoHoursAfter = 120;
	// 判断当前时间是否在三餐后的时间窗口内
	let isWithinOneHourAfterMeal = false;
	let isWithinTwoHoursAfterMeal = false;
	// 检查早餐后的时间窗口
	if (isInTimeWindow(currentHour, currentMinute, breakfastTime, breakfastTime + oneHourAfter)) {
		isWithinOneHourAfterMeal = true;
	} else if (isInTimeWindow(currentHour, currentMinute, breakfastTime, breakfastTime + twoHoursAfter)) {
		isWithinTwoHoursAfterMeal = true;
	}
	// 检查午餐后的时间窗口
	if (!isWithinOneHourAfterMeal && !isWithinTwoHoursAfterMeal && isInTimeWindow(currentHour, currentMinute,
			lunchTime, lunchTime + oneHourAfter)) {
		isWithinOneHourAfterMeal = true;
	} else if (!isWithinOneHourAfterMeal && !isWithinTwoHoursAfterMeal && isInTimeWindow(currentHour,
			currentMinute, lunchTime, lunchTime + twoHoursAfter)) {
		isWithinTwoHoursAfterMeal = true;
	}
	// 检查晚餐后的时间窗口
	if (!isWithinOneHourAfterMeal && !isWithinTwoHoursAfterMeal && isInTimeWindow(currentHour, currentMinute,
			dinnerTime, dinnerTime + oneHourAfter)) {
		isWithinOneHourAfterMeal = true;
	} else if (!isWithinOneHourAfterMeal && !isWithinTwoHoursAfterMeal && isInTimeWindow(currentHour,
			currentMinute, dinnerTime, dinnerTime + twoHoursAfter)) {
		isWithinTwoHoursAfterMeal = true;
	}
	// 如果不在三餐后的时间窗口内，则认为是空腹时间
	const isEmpty = !isWithinOneHourAfterMeal && !isWithinTwoHoursAfterMeal;
	// 根据时间段判断血糖值是否偏高或偏低
	let result;
	if (isWithinOneHourAfterMeal) {
		if (bloodSugarValue > standards.upAfter1) {
			result = {
				mealType:'餐后一小时',
				bloodSugarLevel:'偏高'
			};
		} else if (bloodSugarValue < standards.downAfter1) {
			result = {
				mealType:'餐后一小时',
				bloodSugarLevel:'偏低'
			};
		} else {
			result = {
				mealType:'餐后一小时',
				bloodSugarLevel:'正常'
			};
		}
	} else if (isWithinTwoHoursAfterMeal) {
		if (bloodSugarValue > standards.upAfter2) {
			result = {
				mealType:'餐后两小时',
				bloodSugarLevel:'偏高'
			};
		} else if (bloodSugarValue < standards.downAfter2) {
			result = {
				mealType:'餐后两小时',
				bloodSugarLevel:'偏低'
			};
		} else {
			result =  {
				mealType:'餐后两小时',
				bloodSugarLevel:'正常'
			};
		}
	} else if (isEmpty) {
		if (bloodSugarValue > standards.upEmpty) {
			result = {
				mealType:'空腹',
				bloodSugarLevel:'偏高'
			};
		} else if (bloodSugarValue < standards.downEmpty) {
			result =  {
				mealType:'空腹',
				bloodSugarLevel:'偏低'
			};
		} else {
			result =  {
				mealType:'空腹',
				bloodSugarLevel:'正常'
			};
		}
	} else {
		result = {
			mealType:'未知',
			bloodSugarLevel:'未知'
		}; // 理论上不应该出现这种情况，除非输入数据有误
	}

	return result;
}
// 辅助函数：将时间字符串转换为分钟数（从午夜开始）
const parseBreakfastTime = (timeString) => {
	const [hours, minutes] = timeString.split(':').map(Number);
	return hours * 60 + minutes;
}
// 辅助函数：判断当前时间是否在指定的时间窗口内
const isInTimeWindow = (currentHour, currentMinute, startTime, endTime) => {
	const currentTime = currentHour * 60 + currentMinute;
	return currentTime >= startTime && currentTime < endTime;
}
export  {
	checkBloodSugarStatus
}